####CONDITIONAL MARGINAL EFFECT OF UNAUTHORIZED PROTEST TREATMENT####
coefficients <- read.csv("Code/Book1.csv") ##Read CME estimated with Delta method in Stata
theme_set(theme_minimal() +
            theme(panel.border = element_rect(fill = NA)))

hues <- seq(15, 375, length = 3 + 1)
cols <-  hcl(h = hues, l = 65, c = 100)[c(1, 2)]

names(cols) <- c("Protester approval", "Protest support")
shapes <- c(15, 18)
names(shapes) <- c("Protester approval", "Protest support")

# make figures
coefficients$QOI <- factor(coefficients$QOI, levels=c("Protesters peaceful", "Protesters violent"))
coefficients$Outcome <- factor(coefficients$Outcome, levels=c("Protester approval", "Protest support"))

first_margins<-ggplot(coefficients[is.element(coefficients$Outcome,c("Protester approval",
                                                                     "Protest support")),], aes(x = QOI, y = Unauthorized, group = Outcome,
                                                                                                colour = Outcome, pch = Outcome)) +
  geom_hline(yintercept = 0, linetype = 2, size = 1, colour = "black") +
  geom_point(size = 5, position = position_dodge(width = 0.5)) +
  geom_errorbar(size = 0.5, aes(ymin = lower_ci,
                                ymax = upper_ci),
                position = position_dodge(width = 0.5), width = 0) +
  xlab("") +
  ylab("") +
  labs(color = "Outcome", shape = "Outcome") +
  scale_color_manual(values = cols[c("Protester approval","Protest support")]) +
  scale_shape_manual(values = shapes[c("Protester approval","Protest support")]) +
  theme(axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 0.5)) 
#)

first_margins
fm<-first_margins + theme(legend.position = "none") + 
  theme(axis.text.x = element_text(size = 10))  +
  theme(axis.text.y = element_text(size = 14))  +
  scale_color_grey(start=0.5, end=0.3) +
  ggtitle("CME of Unauthorized Protest")

fm

######

coefficients <- read.csv("Code/Book4.csv") ##Read CME estimated with Delta method in Stata
theme_set(theme_minimal() +
            theme(panel.border = element_rect(fill = NA)))

hues <- seq(15, 375, length = 3 + 1)
cols <-  hcl(h = hues, l = 65, c = 100)[c(1, 2)]

names(cols) <- c("Protester approval", "Protest support")
shapes <- c(15, 18)
names(shapes) <- c("Protester approval", "Protest support")

# make figures
coefficients$QOI <- factor(coefficients$QOI, levels=c("No Arrests", "Arrests"))
coefficients$Outcome <- factor(coefficients$Outcome, levels=c("Protester approval", "Protest support"))

second_margins<-ggplot(coefficients[is.element(coefficients$Outcome,c("Protester approval",
                                                                     "Protest support")),], aes(x = QOI, y = Unauthorized, group = Outcome,
                                                                                                colour = Outcome, pch = Outcome)) +
  geom_hline(yintercept = 0, linetype = 2, size = 1, colour = "black") +
  geom_point(size = 5, position = position_dodge(width = 0.5)) +
  geom_errorbar(size = 0.5, aes(ymin = lower_ci,
                                ymax = upper_ci),
                position = position_dodge(width = 0.5), width = 0) +
  xlab("") +
  ylab("") +
  labs(color = "Outcome", shape = "Outcome") +
  scale_color_manual(values = cols[c("Protester approval","Protest support")]) +
  scale_shape_manual(values = shapes[c("Protester approval","Protest support")]) +
  theme(axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 0.5)) 
#)

second_margins
sm<-second_margins + theme(legend.position = "none") + 
  theme(axis.text.x = element_text(size = 10))  +
  theme(axis.text.y = element_text(size = 14))  +
  scale_color_grey(start=0.5, end=0.3) +
  ggtitle("CME of Unauthorized Protest")

sm

######
coefficients <- read.csv("Code/Book2.csv") ##Read CME estimated with Delta method in Stata
theme_set(theme_minimal() +
            theme(panel.border = element_rect(fill = NA)))

hues <- seq(15, 375, length = 3 + 1)
cols <-  hcl(h = hues, l = 65, c = 100)[c(1, 2)]

names(cols) <- c("Protester approval", "Protest support")
shapes <- c(15, 18)
names(shapes) <- c("Protester approval", "Protest support")

# make figures
coefficients$QOI <- factor(coefficients$QOI, levels=c("Protest authorized", "Protest unauthorized"))
coefficients$Outcome <- factor(coefficients$Outcome, levels=c("Protester approval", "Protest support"))

third_margins<-ggplot(coefficients[is.element(coefficients$Outcome,c("Protester approval",
                                                                      "Protest support")),], aes(x = QOI, y = Violent.tactics, group = Outcome,
                                                                                                 colour = Outcome, pch = Outcome)) +
  geom_hline(yintercept = 0, linetype = 2, size = 1, colour = "black") +
  geom_point(size = 5, position = position_dodge(width = 0.5)) +
  geom_errorbar(size = 0.5, aes(ymin = lower_ci,
                                ymax = upper_ci),
                position = position_dodge(width = 0.5), width = 0) +
  xlab("") +
  ylab("") +
  labs(color = "Outcome", shape = "Outcome") +
  scale_color_manual(values = cols[c("Protester approval","Protest support")]) +
  scale_shape_manual(values = shapes[c("Protester approval","Protest support")]) +
  theme(axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 0.5)) 
#)

third_margins
tm<-third_margins + theme(legend.position = "none") + 
  theme(axis.text.x = element_text(size = 10))  +
  theme(axis.text.y = element_text(size = 14))  +
  scale_color_grey(start=0.5, end=0.3) +
  ggtitle("CME of Protester Violence")

tm

########
coefficients <- read.csv("Code/Book3.csv") ##Read CME estimated with Delta method in Stata
theme_set(theme_minimal() +
            theme(panel.border = element_rect(fill = NA)))

hues <- seq(15, 375, length = 3 + 1)
cols <-  hcl(h = hues, l = 65, c = 100)[c(1, 2)]

names(cols) <- c("Protester approval", "Protest support")
shapes <- c(15, 18)
names(shapes) <- c("Protester approval", "Protest support")

# make figures
coefficients$QOI <- factor(coefficients$QOI, levels=c("Protest authorized", "Protest unauthorized"))
coefficients$Outcome <- factor(coefficients$Outcome, levels=c("Protester approval", "Protest support"))

fourth_margins<-ggplot(coefficients[is.element(coefficients$Outcome,c("Protester approval",
                                                                     "Protest support")),], aes(x = QOI, y = Police.arrests, group = Outcome,
                                                                                                colour = Outcome, pch = Outcome)) +
  geom_hline(yintercept = 0, linetype = 2, size = 1, colour = "black") +
  geom_point(size = 5, position = position_dodge(width = 0.5)) +
  geom_errorbar(size = 0.5, aes(ymin = lower_ci,
                                ymax = upper_ci),
                position = position_dodge(width = 0.5), width = 0) +
  xlab("") +
  ylab("") +
  labs(color = "Outcome", shape = "Outcome") +
  scale_color_manual(values = cols[c("Protester approval","Protest support")]) +
  scale_shape_manual(values = shapes[c("Protester approval","Protest support")]) +
  theme(axis.title.y = element_text(angle = 0, vjust = 0.5, hjust = 0.5)) 
#)

fourth_margins
fom<-fourth_margins + theme(legend.position = "none") + 
  theme(axis.text.x = element_text(size = 10))  +
  theme(axis.text.y = element_text(size = 14))  +
  scale_color_grey(start=0.5, end=0.3) +
  ggtitle("CME of Police Arrests")
fom

ggarrange(first_margins, second_margins, third_margins, fourth_margins, ncol=2, nrow=2, common.legend = TRUE, legend="bottom")
interact<-ggarrange(fm, tm, sm,  fom, ncol=2, nrow=2, common.legend = TRUE, legend="bottom")
interact

ggsave(height=8,
       width=8,
       "Figures/Figure_3.jpg",
       interact)

